package com.simtop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.*;
import com.simtop.pojo.Momo;
import com.simtop.pojo.experiment.ExperimentDynamicVo;
import com.simtop.service.ExperimentService;
import com.simtop.util.JwtUtil;
import com.simtop.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;

/**
 * @author lvyongwei
 * 前端U3D交互
 */
@Controller
@CrossOrigin
@RequestMapping("/unity")
public class U3dController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ExperimentService experimentService;

    /**
     * 记录场景信息
     * @param modelData
     * @param request
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ServerResponse add(@RequestBody ModelData modelData, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            redisUtil.set(u.getId() +":"+ u.getMillTime() + ":" + modelData.getId(),modelData,10*60*60*1000L);
            return ServerResponse.createBySuccessMsg("提交内容成功");
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("提交内容失败");
        }
    }

    /**
     * 张震
     * @param
     * @param
     * @return
     */
    @PostMapping("/addUmodel")
    @ResponseBody
    public ServerResponse addUmodel(@RequestBody Momo momo, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            redisUtil.set(u.getId() +":"+ u.getMillTime(),momo,10*60*60*1000L);
            return ServerResponse.createBySuccessMsg("提交内容成功");
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("提交内容失败");
        }
    }

    /**
     * 记录场景信息
     * @param request
     * @return
     */
    @PostMapping("/refresh")
    @ResponseBody
    public ServerResponse refresh(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            ModelData modelData = new ModelData(1, 0, 0);
            ModelData modelData1 = new ModelData(2, 0, 0);
            redisUtil.set(u.getId() +":"+ u.getMillTime() + ":" + 1,modelData,10*60*60*1000L);
            redisUtil.set(u.getId() +":"+ u.getMillTime() + ":" + 2,modelData1,10*60*60*1000L);
            return ServerResponse.createBySuccessMsg("提交内容成功");
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("提交内容失败");
        }
    }


    /**
     * 获取场景信息
     * @param modelData
     * @param request
     * @return
     */
    @PostMapping("/get")
    @ResponseBody
    public ServerResponse get(@RequestBody ModelData modelData, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            ModelData data = (ModelData) redisUtil.get(u.getId() +":"+ u.getMillTime() + ":" + modelData.getId());
            return ServerResponse.createBySuccess("获取内容成功",data);
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("获取内容失败");
        }
    }

    @PostMapping("/getMd")
    @ResponseBody
    public ServerResponse getMd( HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            Momo data = (Momo) redisUtil.get(u.getId() +":"+ u.getMillTime());
            return ServerResponse.createBySuccess("获取内容成功",data);
        } catch (Exception e){
            return ServerResponse.createByErrorMsg("获取内容失败");
        }
    }

    /**
     * u3d 长新瑞
     * @param
     * @param
     * @return
     */
    @PostMapping("/addu3d")
    @ResponseBody
    public ServerResponse addu3d(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }

//        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        int id = u.getId();
//        String username = u.getUsername();
//        //拿到第一模块
//        Object oneModel = data.get(0);
//        List<Object> one = (List<Object>) oneModel;
//        Object one0 = one.get(0);
//        Object one1 = one.get(1);
//        Object one2 = one.get(2);
//        Object one3 = one.get(3);
//        Object one4 = one.get(4);
//        long l1 = Long.valueOf(String.valueOf(one1));
//        long l2= Long.valueOf(String.valueOf(one2));
//        Date d = new Date(l1);
//        Date d2 = new Date(l2);
//        String oneformat1 = sf.format(d);
//        String oneformat2 = sf.format(d2);
//        //拿到第二模块
//        Object twoModel = data.get(1);
//        List<Object> two = (List<Object>) twoModel;
//        Object two0 = two.get(0);
//        Object two1 = two.get(1);
//        Object two2 = two.get(2);
//        Object two3 = two.get(3);
//        Object two4 = two.get(4);
//        long l1two = Long.valueOf(String.valueOf(two1));
//        long l2two= Long.valueOf(String.valueOf(two2));
//        Date dtwo = new Date(l1two);
//        Date d2two = new Date(l2two);
//        String twoformat1 = sf.format(dtwo);
//        String twoformat2 = sf.format(d2two);
//        //拿到第三模块
//        Object threeModel = data.get(2);
//        List<Object> three = (List<Object>) threeModel;
//        Object three0 = three.get(0);
//        Object three1 = three.get(1);
//        Object three2 = three.get(2);
//        Object three3 = three.get(3);
//        Object three4 = three.get(4);
//        long l1three = Long.valueOf(String.valueOf(three1));
//        long l2three= Long.valueOf(String.valueOf(three2));
//        Date dthree = new Date(l1three);
//        Date d2dthree = new Date(l2three);
//        String threeformat1 = sf.format(dthree);
//        String threeformat2 = sf.format(d2dthree);
//        //拿到第四模块
//        Object forModel = data.get(3);
//        List<Object> fors = (List<Object>) forModel;
//        Object for0 = fors.get(0);
//        Object for1 = fors.get(1);
//        Object for2 = fors.get(2);
//        Object for3 = fors.get(3);
//        Object for4 = fors.get(4);
//        long l1for = Long.valueOf(String.valueOf(for1));
//        long l2for= Long.valueOf(String.valueOf(for2));
//        Date dfor = new Date(l1for);
//        Date d2for = new Date(l2for);
//        String forformat1 = sf.format(dfor);
//        String forformat2 = sf.format(d2for);
//        //拿到第五模块
//        Object fiveModel = data.get(4);
//        List<Object> five = (List<Object>) fiveModel;
//        Object five0 = five.get(0);
//        Object five1 = five.get(1);
//        Object five2 = five.get(2);
//        Object five3 = five.get(3);
//        Object five4 = five.get(4);
//        long l1five = Long.valueOf(String.valueOf(five1));
//        long l2five= Long.valueOf(String.valueOf(five2));
//        Date dfive = new Date(l1five);
//        Date d2five = new Date(l2five);
//        String fiveformat1 = sf.format(dfive);
//        String fiveformat2 = sf.format(d2five);
//        //拿到第六模块
//        Object sixModel = data.get(5);
//        List<Object> six = (List<Object>) sixModel;
//        Object six0 = six.get(0);
//        Object six1 = six.get(1);
//        Object six2 = six.get(2);
//        Object six3 = six.get(3);
//        Object six4 = six.get(4);
//        long l1six = Long.valueOf(String.valueOf(six1));
//        long l2six= Long.valueOf(String.valueOf(six2));
//        Date dsix = new Date(l1six);
//        Date d2six = new Date(l2six);
//        String sixformat1 = sf.format(dsix);
//        String sixformat2 = sf.format(d2six);
//        //拿到第七模块
//        Object sevenModel = data.get(6);
//        List<Object> seven = (List<Object>) sevenModel;
//        Object seven0 = seven.get(0);
//        Object seven1 = seven.get(1);
//        Object seven2 = seven.get(2);
//        Object seven3 = seven.get(3);
//        Object seven4 = seven.get(4);
//        long l1seven = Long.valueOf(String.valueOf(seven1));
//        long l2seven= Long.valueOf(String.valueOf(seven2));
//        Date dseven = new Date(l1seven);
//        Date d2seven = new Date(l2seven);
//        String sevenformat1 = sf.format(dseven);
//        String sevenformat2 = sf.format(d2seven);
//        //拿到第八模块
//        Object eightModel = data.get(7);
//        List<Object> eight = (List<Object>) eightModel;
//        Object eight0 = eight.get(0);
//        Object eight1 = eight.get(1);
//        Object eight2 = eight.get(2);
//        Object eight3 = eight.get(3);
//        Object eight4 = eight.get(4);
//        long l1eight = Long.valueOf(String.valueOf(eight1));
//        long l2eight = Long.valueOf(String.valueOf(eight2));
//        Date deight = new Date(l1eight);
//        Date d2eight = new Date(l2eight);
//        String eightformat1 = sf.format(deight);
//        String eightformat2 = sf.format(d2eight);
//        //拿到第九模块
//        Object nineModel = data.get(8);
//        List<Object> nine = (List<Object>) nineModel;
//        Object nine0 = nine.get(0);
//        Object nine1 = nine.get(1);
//        Object nine2 = nine.get(2);
//        Object nine3 = nine.get(3);
//        Object nine4 = nine.get(4);
//        long l1nine = Long.valueOf(String.valueOf(nine1));
//        long l2nine= Long.valueOf(String.valueOf(nine2));
//        Date dnine = new Date(l1nine);
//        Date d2nine = new Date(l2nine);
//        String nineformat1 = sf.format(dnine);
//        String nineformat2 = sf.format(d2nine);
//        //拿到第十模块
//        Object tenModel = data.get(9);
//        List<Object> ten = (List<Object>) tenModel;
//        Object ten0 = ten.get(0);
//        Object ten1 = ten.get(1);
//        Object ten2 = ten.get(2);
//        Object ten3 = ten.get(3);
//        Object ten4 = ten.get(4);
//        long l1ten = Long.valueOf(String.valueOf(ten1));
//        long l2ten = Long.valueOf(String.valueOf(ten2));
//        Date dten = new Date(l1ten);
//        Date d2ten = new Date(l2ten);
//        String tenformat1 = sf.format(dten);
//        String tenformat2 = sf.format(d2ten);
//
//        //拿到第11模块eleve
//        Object eleveModel = data.get(9);
//        List<Object> eleve = (List<Object>) eleveModel;
//        Object eleve0 = eleve.get(0);
//        Object eleve1 = eleve.get(1);
//        Object eleve2 = eleve.get(2);
//        Object eleve3 = eleve.get(3);
//        Object eleve4 = eleve.get(4);
//        long l1eleve = Long.valueOf(String.valueOf(eleve1));
//        long l2eleve = Long.valueOf(String.valueOf(eleve2));
//        Date deleve = new Date(l1eleve);
//        Date d2eleve = new Date(l2eleve);
//        String eleveformat1 = sf.format(deleve);
//        String eleveformat2 = sf.format(d2eleve);
//
//        //拿到第12模块 twelve
//        Object twelveModel = data.get(9);
//        List<Object> twelve = (List<Object>) twelveModel;
//        Object twelve0 = twelve.get(0);
//        Object twelve1 = twelve.get(1);
//        Object twelve2 = twelve.get(2);
//        Object twelve3 = twelve.get(3);
//        Object twelve4 = twelve.get(4);
//        long l1twelve = Long.valueOf(String.valueOf(twelve1));
//        long l2twelve = Long.valueOf(String.valueOf(twelve2));
//        Date dtwelve = new Date(l1twelve);
//        Date d2twelve = new Date(l2twelve);
//        String twelveformat1 = sf.format(dtwelve);
//        String twelveformat2 = sf.format(d2twelve);
//
//        //拿到第13模块  thirteen
//        Object thirteenModel = data.get(9);
//        List<Object> thirteen = (List<Object>) thirteenModel;
//        Object thirteen0 = thirteen.get(0);
//        Object thirteen1 = thirteen.get(1);
//        Object thirteen2 = thirteen.get(2);
//        Object thirteen3 = thirteen.get(3);
//        Object thirteen4 = thirteen.get(4);
//        long l1thirteen = Long.valueOf(String.valueOf(thirteen1));
//        long l2thirteen = Long.valueOf(String.valueOf(thirteen2));
//        Date dthirteen = new Date(l1thirteen);
//        Date d2thirteen = new Date(l2thirteen);
//        String thirteenformat1 = sf.format(dthirteen);
//        String thirteenformat2 = sf.format(d2thirteen);
//
//        //拿到第14模块ffourteen
//        Object fourteenModel = data.get(9);
//        List<Object> fourteen = (List<Object>) fourteenModel;
//        Object fourteen0 = fourteen.get(0);
//        Object fourteen1 = fourteen.get(1);
//        Object fourteen2 = fourteen.get(2);
//        Object fourteen3 = fourteen.get(3);
//        Object fourteen4 = fourteen.get(4);
//        long l1fourteen = Long.valueOf(String.valueOf(fourteen1));
//        long l2fourteen = Long.valueOf(String.valueOf(fourteen2));
//        Date dfourteen = new Date(l1fourteen);
//        Date d2fourteen = new Date(l2fourteen);
//        String fourteenformat1 = sf.format(dfourteen);
//        String fourteenformat2 = sf.format(d2fourteen);
//
//        //拿到第15模块 fifteen
//        Object fifteenModel = data.get(9);
//        List<Object> fifteen = (List<Object>) fifteenModel;
//        Object fifteen0 = fifteen.get(0);
//        Object fifteen1 = fifteen.get(1);
//        Object fifteen2 = fifteen.get(2);
//        Object fifteen3 = fifteen.get(3);
//        Object fifteen4 = fifteen.get(4);
//        long l1fifteen = Long.valueOf(String.valueOf(fifteen1));
//        long l2fifteen = Long.valueOf(String.valueOf(fifteen2));
//        Date dfifteen = new Date(l1fifteen);
//        Date d2fifteen = new Date(l2fifteen);
//        String fifteenformat1 = sf.format(dfifteen);
//        String fifteenformat2 = sf.format(d2fifteen);
//
//        //拿到第16模块 sixteen
//        Object sixteenModel = data.get(9);
//        List<Object> sixteen = (List<Object>) sixteenModel;
//        Object sixteen0 = sixteen.get(0);
//        Object sixteen1 = sixteen.get(1);
//        Object sixteen2 = sixteen.get(2);
//        Object sixteen3 = sixteen.get(3);
//        Object sixteen4 = sixteen.get(4);
//        long l1sixteen = Long.valueOf(String.valueOf(sixteen1));
//        long l2sixteen = Long.valueOf(String.valueOf(sixteen2));
//        Date dsixteen = new Date(l1sixteen);
//        Date d2sixteen = new Date(l2sixteen);
//        String sixteenformat1 = sf.format(dsixteen);
//        String sixteenformat2 = sf.format(d2sixteen);
//
//        //拿到第17模块 seventeen
//        Object seventeenModel = data.get(9);
//        List<Object> seventeen = (List<Object>) seventeenModel;
//        Object seventeen0 = seventeen.get(0);
//        Object seventeen1 = seventeen.get(1);
//        Object seventeen2 = seventeen.get(2);
//        Object seventeen3 = seventeen.get(3);
//        Object seventeen4 = seventeen.get(4);
//        long l1seventeen = Long.valueOf(String.valueOf(seventeen1));
//        long l2seventeen = Long.valueOf(String.valueOf(seventeen2));
//        Date dseventeen = new Date(l1seventeen);
//        Date d2seventeen = new Date(l2seventeen);
//        String seventeenformat1 = sf.format(dseventeen);
//        String seventeenformat2 = sf.format(d2seventeen);
//
//        //拿到第18模块eighteen
//        Object eighteenModel = data.get(9);
//        List<Object> eighteen = (List<Object>) eighteenModel;
//        Object eighteen0 = eighteen.get(0);
//        Object eighteen1 = eighteen.get(1);
//        Object eighteen2 = eighteen.get(2);
//        Object eighteen3 = eighteen.get(3);
//        Object eighteen4 = eighteen.get(4);
//        long l1eighteen = Long.valueOf(String.valueOf(eighteen1));
//        long l2eighteen = Long.valueOf(String.valueOf(eighteen2));
//        Date deighteen = new Date(l1eighteen);
//        Date d2eighteen = new Date(l2eighteen);
//        String eighteenformat1 = sf.format(deighteen);
//        String eighteenformat2 = sf.format(d2eighteen);
//
//        //拿到第19模块nineteen
//        Object nineteenModel = data.get(9);
//        List<Object> nineteen = (List<Object>) nineteenModel;
//        Object nineteen0 = nineteen.get(0);
//        Object nineteen1 = nineteen.get(1);
//        Object nineteen2 = nineteen.get(2);
//        Object nineteen3 = nineteen.get(3);
//        Object nineteen4 = nineteen.get(4);
//        long l1nineteen = Long.valueOf(String.valueOf(nineteen1));
//        long l2nineteen = Long.valueOf(String.valueOf(nineteen2));
//        Date dnineteen = new Date(l1nineteen);
//        Date d2nineteen = new Date(l2nineteen);
//        String nineteenformat1 = sf.format(dnineteen);
//        String nineteenformat2 = sf.format(d2nineteen);
//
//        //拿到第20模块
//        Object twentyModel = data.get(9);
//        List<Object> twenty = (List<Object>) twentyModel;
//        Object twenty0 = twenty.get(0);
//        Object twenty1 = twenty.get(1);
//        Object twenty2 = twenty.get(2);
//        Object twenty3 = twenty.get(3);
//        Object twenty4 = twenty.get(4);
//        long l1twenty = Long.valueOf(String.valueOf(twenty1));
//        long l2twenty = Long.valueOf(String.valueOf(twenty2));
//        Date dtwenty = new Date(l1twenty);
//        Date d2twenty = new Date(l2twenty);
//        String twentyformat1 = sf.format(dtwenty);
//        String twentyformat2 = sf.format(d2twenty);
//        //lasg模块
//        Object lastModel = data.get(10);
//        List<Object> last = (List<Object>) lastModel;
//        Object last0 = last.get(0);
//        Object last1 = last.get(1);
//        Object last2 = last.get(2);
//        Object last3 = last.get(3);
//        Object last4 = last.get(4);
//        long l1last = Long.valueOf(String.valueOf(last1));
//        long l2last = Long.valueOf(String.valueOf(last2));
//        Date dlast = new Date(l1last);
//        Date d2last = new Date(l2last);
//        String lastformat1 = sf.format(dlast);
//        String lastformat2 = sf.format(d2last);
//        experimentService.addud(id,username,one0,oneformat1,oneformat2,one3,one4,
//                two0,twoformat1,twoformat2,two3,two4,
//                three0,threeformat1,threeformat2,three3,three4,
//                for0,forformat1,forformat2,for3,for4,
//                five0,fiveformat1,fiveformat2,five3,five4,
//                six0,sixformat1,sixformat2,six3,six4,
//                seven0,sevenformat1,sevenformat2,seven3,seven4,
//                eight0,eightformat1,eightformat2,eight3,eight4,
//                nine0,nineformat1,nineformat2,nine3,nine4,
//                ten0,tenformat1,tenformat2,ten3,ten4,
//                eleve0,eleveformat1,eleveformat2,eleve3,eleve4,
//                twelve0,twelveformat1,twelveformat2,twelve3,twelve4,
//                thirteen0,thirteenformat1,thirteenformat2,thirteen3,thirteen4,
//                fourteen0,fourteenformat1,fourteenformat2,fourteen3,fourteen4,
//                fifteen0,fifteenformat1,fifteenformat2,fifteen3,fifteen4,
//                sixteen0,sixteenformat1,sixteenformat2,sixteen3,sixteen4,
//                seventeen0,seventeenformat1,seventeenformat2,seventeen3,seventeen4,
//                eighteen0,eighteenformat1,eighteenformat2,eighteen3,eighteen4,
//                nineteen0,nineteenformat1,nineteenformat2,nineteen3,nineteen4,
//                twenty0,twentyformat1,twentyformat2,twenty3,twenty4,
//                last0,lastformat1,lastformat2,last3,last4
//
//        );

        int id = u.getId();
        String username = u.getUsername();

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(formatter.format(date));
        String formatdata = formatter.format(date);
        experimentService.addu3d02(id,username,data,formatdata);

        return ServerResponse.createBySuccessMsg("提交成绩成功");

    }
    @PostMapping("/findExperimental")
    @ResponseBody
    public ServerResponse findExperimental(@RequestBody ExperimentDynamicVo experimentDynamicVo, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        //System.out.println(u.getId());
        //System.out.println(u.getRoleId());

        //判断不是管理员和教师身份
        if (!u.getRoleId().equals(1) && !u.getRoleId().equals(2)) {
            PageHelper.startPage(experimentDynamicVo.getPageNum(),experimentDynamicVo.getPageSize());
            //只找自己\
            System.out.println(u.getId());
            List<Uds> allsy2 = experimentService.allsy2(u.getId());
            System.out.println(allsy2);
            PageInfo<Uds> pageInfo = new PageInfo<Uds>(allsy2);
            return ServerResponse.createBySuccess("获取实验报告成功",pageInfo);
        }
        PageHelper.startPage(experimentDynamicVo.getPageNum(),experimentDynamicVo.getPageSize());
        //List<ExperimentReportVo> allReport = experimentService.getAllReport(null,experimentDynamicVo.getMessage());
        //全查
        List<Uds> allallsy02 = experimentService.allallsy02();
        PageInfo<Uds> pageInfo = new PageInfo<Uds>(allallsy02);
        return ServerResponse.createBySuccess("获取实验报告成功",pageInfo);

    }


    @PostMapping("/deleteExperimental")
    @ResponseBody
    public ServerResponse deleteExperimental(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        JSONObject jsonObject = JSON.parseObject(data);
        Integer udId = jsonObject.getInteger("udId");
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        //System.out.println(u.getId());
        //System.out.println(u.getRoleId());

        //判断不是管理员和教师身份
        if (!u.getRoleId().equals(1) && !u.getRoleId().equals(2)) {

            return ServerResponse.createByErrorMsg("无权限");
        }

        experimentService.deleteExperimental(udId);
        return ServerResponse.createBySuccess("删除实验报告成功");

    }

}
